package com.cbgolf.oa.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.entity.PermissionsBean;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.widget.ListViewNew;
import com.cbgolf.oa.widget.autolayout.utils.AutoUtil;
import com.zhy.base.adapter.ViewHolder;
import com.zhy.base.adapter.abslistview.CommonAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lq on 2017/9/18.
 */

public class PermissionHistoryAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater mInflater;
    private List<PermissionsBean> list = new ArrayList<>();

    public PermissionHistoryAdapter(Context context) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
    }

    public void clear() {
        if (null != list && list.size() > 0) {
            this.list.clear();
        }
    }

    public void addALL(List<PermissionsBean> list) {
        this.list.addAll(list);
    }

    public void addData(List<PermissionsBean> list) {
        this.list.addAll(list);
    }


    public void setData(List<PermissionsBean> ls) {
        this.list = ls;
    }

    public void upDataUI() {
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        Holder h = null;
        if (null == view) {
            h = new Holder();
            view = mInflater.inflate(R.layout.item_permission_history, null);
            h.dateBarView = view.findViewById(R.id.item_per_history_date_ll);
            h.dateBarTv = (TextView) view.findViewById(R.id.item_per_history_date_tv);
            h.modelNameTv = (TextView) view.findViewById(R.id.item_per_history_model_name_tv);
            h.requestTimeTv = (TextView) view.findViewById(R.id.item_per_history_request_time_tv);
            h.requestCauseTv = (TextView) view.findViewById(R.id.item_per_history_cause_tv);
            h.requestUserNameTv = (TextView) view.findViewById(R.id.item_per_history_request_user_name_tv);
            h.listViewCardNum = (ListViewNew) view.findViewById(R.id.item_per_history_code_listview);
            h.listViewNames = (ListViewNew) view.findViewById(R.id.item_per_history_destroy_name_listview);
            h.cardNumTv = (TextView) view.findViewById(R.id.item_per_history_code_tv);
            h.nameTv = (TextView) view.findViewById(R.id.item_per_history_destroy_name_tv);
            h.allowUserNameTv = (TextView) view.findViewById(R.id.item_per_history_allow_user_name_tv);
            h.resultTv = (TextView) view.findViewById(R.id.item_per_history_result_tv);
            h.allowUseTimeTv = (TextView) view.findViewById(R.id.item_per_history_allow_use_time_tv);
            h.allowUseTimeView = view.findViewById(R.id.item_per_history_allow_use_time_ll);
            h.allowTimeTv = (TextView) view.findViewById(R.id.item_per_history_result_time_tv);
            view.setTag(h);
        } else {
            h = (Holder) view.getTag();
        }
        AutoUtil.auto(view);

        PermissionsBean m = list.get(position);
        if (null == m) {
            return view;
        }
        String type = m.auditType;
        String requestTime = m.applyTime;
        String requestUserName = m.applyPerson;
        String requestCause = m.applyReason;
        String cardNum = m.cardNo;
        String usedName = m.consumptionPerson;

        String resultTime = m.auditTime;
        boolean result = m.auditSatus;
        String allowUserName = m.auditName;
//        String allowUseTime = m.auditName;
        String resushCause = m.refuseReason;
//        if (!Util.isNull(type) && "CHANGEREGISTER".equals(type)) {
//            h.modelNameTv.setText("修改\n结账");
//        } else {
//            h.modelNameTv.setText("优惠\n券    ");
//        }
        h.modelNameTv.setText(getType(type));
        h.listViewCardNum.setVisibility(Util.isEquals("SIGN", type) ? View.VISIBLE : View.GONE);
        h.listViewNames.setVisibility(Util.isEquals("SIGN", type) ? View.VISIBLE : View.GONE);
        if (Util.isEquals("SIGN", type)) {
            if (!Util.listIsNull(m.cardNos)) {
                setDataAdapter(h.listViewCardNum, m.cardNos);
            }
            if (!Util.listIsNull(m.consumptionPersons)) {
                setDataAdapter(h.listViewNames, m.consumptionPersons);
            }
        }
        h.cardNumTv.setText(TextUtil.Text(cardNum));
        h.nameTv.setText(TextUtil.Text(usedName));
        h.cardNumTv.setVisibility(!Util.isEquals("SIGN", type) ? View.VISIBLE : View.GONE);
        h.nameTv.setVisibility(!Util.isEquals("SIGN", type) ? View.VISIBLE : View.GONE);
        h.requestTimeTv.setText(DateUtil.stampToDate(requestTime, "HH:mm:ss"));
        String date = DateUtil.stampToDateMd2(resultTime);
        if (!Util.isNull(date)) {
            int p = -1;
            for (int i = 0; i < getCount(); i++) {
                String s = DateUtil.stampToDateMd2(list.get(i).auditTime);
                if (!Util.isNull(s) && s.equals(date)) {
                    p = i;
                    break;
                }
            }
            if (position == p) {
                h.dateBarView.setVisibility(View.VISIBLE);
                h.dateBarTv.setText(date);
            } else {
                h.dateBarView.setVisibility(View.GONE);
            }
        } else {
            h.dateBarView.setVisibility(View.GONE);
        }
        h.requestUserNameTv.setText(TextUtil.Text(requestUserName));

        h.requestCauseTv.setText(TextUtil.Text(requestCause));

        h.allowTimeTv.setText(DateUtil.stampToDate(resultTime, "HH:mm:ss"));
        h.resultTv.setText(result ? "允许" : "拒绝");
        h.allowUserNameTv.setText(Util.isNull(allowUserName) ? "" : allowUserName);
//        h.allowUseTimeTv.setText("本次");
//        h.allowUseTimeView.setVisibility(View.VISIBLE);
        if (result) {
            h.allowUseTimeView.setVisibility(View.VISIBLE);
            h.allowUseTimeTv.setText("本次");
//            h.refushCauseView.setVisibility(View.GONE);
        } else {
            h.allowUseTimeView.setVisibility(View.GONE);
//            h.refushCauseView.setVisibility(View.VISIBLE);
//            h.refushCauseTv.setText(Util.isNull(resushCause) ? "" : resushCause);
        }
        return view;
    }

    private void setDataAdapter(ListViewNew listview, List<String> list) {
        CommonAdapter<String> adapter = new CommonAdapter<String>(context, R.layout.item_tv, list) {
            @Override
            public void convert(ViewHolder holder, String s) {
                holder.setText(R.id.item_tv, TextUtil.Text(s));
            }
        };
        listview.setAdapter(adapter);
    }

    private String getType(String type) {
        if (Util.isNull(type)) return "";
        String name = "";
        switch (type) {
            case "CHANGEREGISTER":
                name = "修改\n结账";
                break;
            case "CONPON":
                name = "优惠\n券    ";
                break;
            case "SIGN":
                name = "签单\n审批";
                break;
            case "CHANGE_IDENTITY":
                name = "修改\n身份";
                break;
        }
        return name;

    }

    class Holder {
        private View dateBarView;
        private TextView dateBarTv;
        private TextView modelNameTv;
        private TextView requestTimeTv;
        private TextView requestCauseTv;
        private TextView requestUserNameTv;
        private ListViewNew listViewCardNum;
        private ListViewNew listViewNames;
        private TextView cardNumTv;
        private TextView nameTv;

        private TextView allowTimeTv;
        private TextView resultTv;
        private TextView allowUserNameTv;
        private TextView refushCauseTv;
        private View refushCauseView;
        private TextView allowUseTimeTv;
        private View allowUseTimeView;

    }

}

